<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Maximum volume inscribed ellipsoid in a polyhedron</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch08_geometric_probs/html/max_vol_ellip_in_polyhedra.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Maximum volume inscribed ellipsoid in a polyhedron</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 8.4.1, Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Original version by Lieven Vandenberghe</span>
<span class="comment">% Updated for CVX by Almir Mutapcic - Jan 2006</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% We find the ellipsoid E of maximum volume that lies inside of</span>
<span class="comment">% a polyhedra C described by a set of linear inequalities.</span>
<span class="comment">%</span>
<span class="comment">% C = { x | a_i^T x &lt;= b_i, i = 1,...,m } (polyhedra)</span>
<span class="comment">% E = { Bu + d | || u || &lt;= 1 } (ellipsoid)</span>
<span class="comment">%</span>
<span class="comment">% This problem can be formulated as a log det maximization</span>
<span class="comment">% which can then be computed using the det_rootn function, ie,</span>
<span class="comment">%     maximize     log det B</span>
<span class="comment">%     subject to   || B a_i || + a_i^T d &lt;= b,  for i = 1,...,m</span>

<span class="comment">% problem data</span>
n = 2;
px = [0 .5 2 3 1];
py = [0 1 1.5 .5 -.5];
m = size(px,2);
pxint = sum(px)/m; pyint = sum(py)/m;
px = [px px(1)];
py = [py py(1)];

<span class="comment">% generate A,b</span>
A = zeros(m,n); b = zeros(m,1);
<span class="keyword">for</span> i=1:m
  A(i,:) = null([px(i+1)-px(i) py(i+1)-py(i)])';
  b(i) = A(i,:)*.5*[px(i+1)+px(i); py(i+1)+py(i)];
  <span class="keyword">if</span> A(i,:)*[pxint; pyint]-b(i)&gt;0
    A(i,:) = -A(i,:);
    b(i) = -b(i);
  <span class="keyword">end</span>
<span class="keyword">end</span>

<span class="comment">% formulate and solve the problem</span>
cvx_begin
    variable <span class="string">B(n,n)</span> <span class="string">symmetric</span>
    variable <span class="string">d(n)</span>
    maximize( det_rootn( B ) )
    subject <span class="string">to</span>
       <span class="keyword">for</span> i = 1:m
           norm( B*A(i,:)', 2 ) + A(i,:)*d &lt;= b(i);
       <span class="keyword">end</span>
cvx_end

<span class="comment">% make the plots</span>
noangles = 200;
angles   = linspace( 0, 2 * pi, noangles );
ellipse_inner  = B * [ cos(angles) ; sin(angles) ] + d * ones( 1, noangles );
ellipse_outer  = 2*B * [ cos(angles) ; sin(angles) ] + d * ones( 1, noangles );

clf
plot(px,py)
hold <span class="string">on</span>
plot( ellipse_inner(1,:), ellipse_inner(2,:), <span class="string">'r--'</span> );
plot( ellipse_outer(1,:), ellipse_outer(2,:), <span class="string">'r--'</span> );
axis <span class="string">square</span>
axis <span class="string">off</span>
hold <span class="string">off</span>
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling SDPT3: 34 variables, 15 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints = 15
 dim. of sdp    var  =  6,   num. of sdp  blk  =  2
 dim. of socp   var  = 15,   num. of socp blk  =  5
 dim. of linear var  =  6
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|1.8e+01|9.4e+00|1.3e+03| 1.441021e+01  0.000000e+00| 0:0:00| chol  1  1 
 1|1.000|0.628|8.3e-06|3.6e+00|5.4e+02| 5.502362e+01 -5.884367e+00| 0:0:00| chol  1  1 
 2|1.000|0.991|5.8e-06|4.2e-02|4.4e+01| 3.953225e+01 -8.633868e-02| 0:0:00| chol  1  1 
 3|0.915|1.000|7.6e-07|1.0e-03|3.9e+00| 3.947314e+00  4.254554e-02| 0:0:00| chol  1  1 
 4|0.691|1.000|2.8e-07|1.0e-04|1.9e+00| 2.071907e+00  1.972134e-01| 0:0:00| chol  1  1 
 5|0.972|1.000|1.1e-08|1.0e-05|5.5e-01| 1.147511e+00  5.991432e-01| 0:0:00| chol  1  1 
 6|1.000|0.939|1.6e-09|1.6e-06|7.2e-02| 9.841548e-01  9.117756e-01| 0:0:00| chol  1  1 
 7|0.951|0.954|5.3e-10|1.7e-07|3.6e-03| 9.536201e-01  9.500190e-01| 0:0:00| chol  1  1 
 8|0.978|0.975|2.5e-10|1.4e-08|8.8e-05| 9.523368e-01  9.522492e-01| 0:0:00| chol  1  1 
 9|0.955|0.977|4.9e-11|3.7e-10|2.7e-06| 9.523088e-01  9.523061e-01| 0:0:00| chol  1  1 
10|0.987|1.000|6.5e-13|9.8e-12|2.4e-07| 9.523076e-01  9.523074e-01| 0:0:00| chol  1  1 
11|0.993|1.000|5.1e-15|1.0e-12|1.3e-08| 9.523075e-01  9.523075e-01| 0:0:00|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 11
 primal objective value =  9.52307513e-01
 dual   objective value =  9.52307500e-01
 gap := trace(XZ)       = 1.33e-08
 relative gap           = 4.59e-09
 actual relative gap    = 4.59e-09
 rel. primal infeas     = 5.09e-15
 rel. dual   infeas     = 1.00e-12
 norm(X), norm(y), norm(Z) = 2.2e+00, 2.6e+00, 4.6e+00
 norm(A), norm(b), norm(C) = 7.6e+00, 2.0e+00, 3.7e+00
 Total CPU time (secs)  = 0.31  
 CPU time per iteration = 0.03  
 termination code       =  0
 DIMACS: 5.1e-15  0.0e+00  1.1e-12  0.0e+00  4.6e-09  4.6e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.952308
 
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="max_vol_ellip_in_polyhedra__01.png" alt=""> 
</div>
</div>
</body>
</html>